package com.zzu.base.handler;

import com.zzu.base.common.enums.ResultStatusEnum;
import com.zzu.base.common.pojo.ResultResponse;
import com.zzu.base.exception.ProcessException;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@Slf4j
@RestControllerAdvice
public class ExceptionHandler {
    @Resource
    private AccessDeniedExceptionHandler accessDeniedExceptionHandler;
    @org.springframework.web.bind.annotation.ExceptionHandler(Exception.class)
    public ResultResponse<String> handleUnexpectedException(Exception e, HttpServletResponse response) {
        log.error(e.getMessage());
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return ResultResponse.error(e.getMessage(), ResultStatusEnum.INTERNAL_SERVER_ERROR);
    }
    @org.springframework.web.bind.annotation.ExceptionHandler(AccessDeniedException.class)
    public ResultResponse<Object> handleAccessDeniedException(AccessDeniedException e, HttpServletResponse response, HttpServletRequest request) {
        log.error(e.getMessage());
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        return ResultResponse.error(e.getMessage(), ResultStatusEnum.FORBIDDEN);
    }
    @org.springframework.web.bind.annotation.ExceptionHandler(ProcessException.class)
    public ResultResponse<Object> handleProcessException(ProcessException e, HttpServletResponse response) {
        log.error(e.getMessage());
        try {
            HttpStatus.valueOf(e.getStatus().getCode());
            response.setStatus(e.getStatus().getCode());
        }catch (IllegalArgumentException iae){
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
        return ResultResponse.error(e.getData(),e.getStatus());
    }
    @org.springframework.web.bind.annotation.ExceptionHandler(MethodArgumentNotValidException.class)
    public ResultResponse<String> MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e, HttpServletResponse response) {

        log.error(e.getMessage());
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        // 从异常对象中拿到ObjectError对象
        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
        // 然后提取错误提示信息进行返回
        return ResultResponse.error(objectError.getDefaultMessage(),ResultStatusEnum.BAD_REQUEST);
    }

}
